home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / program / misc / systemid.lha / SystemIdle / idle.c next >
C/C++ Source or Header  |  1995-10-11  |  5KB  |  221 lines

  1. /*****************************************************************
  2.  
  3.     SystemIdler V1.0 (Idle counter module for TinyMeter 3.6)  by
  4.     Tinic Urou in 1995, FreeWare, orginal code by Thomas Radtke.
  5.     Use this at your own risk. Please leave me a mail if you are
  6.     using this code in your programs:
  7.  
  8.         EMail: tinic@tinic.mayn.sub.de
  9.  
  10.     I modified the code from cpuload2. The routines to count the
  11.     maximum  are  much  more  better I think, since they use the
  12.     exact same  number  of  cycles  as  the  normal  idle  count
  13.     routine.
  14.     On a A4000/030, disabling the startup-sequence, you will now
  15.     get  1%  usage.  With  all my tools I get 9-12% usage. These
  16.     results should be correct.
  17.  
  18.     Bugs: None known.
  19.     ¯¯¯¯
  20.     Invoking:
  21.     ¯¯¯¯¯¯¯¯
  22.     init_idle(); to setup the idle task
  23.     free_idle(); to remove the idle Task
  24.  
  25.     unsigned long idle;      is the actual idlecount
  26.     unsigned long maximum;   is the maximum idlecount
  27.  
  28.     To get f.ex. the percentage of system usage simply use:
  29.  
  30.     __________________________________________________________
  31.  
  32.         extern unsigned long maximum,idle;
  33.  
  34.         showusage()
  35.         {
  36.             int percent,n;
  37.  
  38.             if(init_idle())
  39.             {
  40.                 for(n=0;n<25;n++)
  41.                 {
  42.                     percent=(int)((idle*100)/maximum);
  43.                     printf("%d percent free\n",percent);
  44.                     Delay(50L);
  45.                 }
  46.                 free_idle();
  47.             }
  48.         }
  49.  
  50.     ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  51.  
  52.     Used compiler:  gcc  2.7.0  with  libnix  1.0  Look  at  the
  53.     makefile for the used options.
  54.  
  55. *******************************************************************/
  56.  
  57. #include <intuition/IntuitionBase.h>
  58. #include <exec/nodes.h>
  59. #include <exec/tasks.h>
  60. #include <libraries/dos.h>
  61. #include <exec/types.h>
  62. #include <exec/memory.h>
  63. #include <stdlib.h>
  64. #include <stdio.h>
  65. #include <signal.h>
  66. #include <exec/libraries.h>
  67. #include <dos/dos.h>
  68.  
  69. extern  struct          Task *FindTask(char *);
  70.         struct          Task *task,*task2,*met;
  71.  
  72.         char            *taskname_1 = "CPU_GET",
  73.                         *taskname_2 = "CPU_SET";
  74.  
  75.         BOOL            quit_setidle,
  76.                         quit_getidle,
  77.                         start_count=TRUE;
  78.  
  79.         unsigned long   maximum,
  80.                         cnt,
  81.                         idle;
  82.  
  83. getidle()
  84. {
  85.     struct          timeval updateval;
  86.     int             n;
  87.     struct MsgPort  *timerport;
  88.     struct          timerequest *tr;
  89.     struct          Task *met2;
  90.  
  91.     /* use "__geta4 getidle()" for DICE or SAS and remove geta4(); */
  92.  
  93.     geta4();
  94.  
  95.     met2=FindTask(NULL);
  96.  
  97.     if ((timerport=(struct MsgPort *)CreatePort (0,0)))
  98.     {
  99.         if ((tr=(struct timerequest *)CreateExtIO(timerport,sizeof(struct timerequest))))
  100.         {
  101.             if ((OpenDevice (TIMERNAME,UNIT_MICROHZ,(struct IORequest *)tr,0))!=0)
  102.             {
  103.                 DeleteExtIO(tr);
  104.                 DeletePort(timerport);
  105.                 goto error;
  106.             }
  107.         }
  108.         else
  109.         {
  110.             DeletePort(timerport);
  111.             goto error;
  112.         }
  113.     }
  114.     else goto error;
  115.  
  116.     updateval.tv_secs =1;
  117.     updateval.tv_micro=0;
  118.  
  119.     while((met2->tc_SigRecvd & SIGBREAKF_CTRL_D)==0)
  120.     {
  121.         /* signal setidle()-task, that we can start counting */
  122.  
  123.         start_count=FALSE;
  124.  
  125.         cnt=0;
  126.         tr->tr_node.io_Command=TR_ADDREQUEST;
  127.         tr->tr_time=updateval;
  128.         DoIO((struct IORequest *)tr);
  129.         idle=cnt;
  130.  
  131.         /* check if we have to setup maximum */
  132.         if(maximum==0)
  133.         {
  134.             maximum=idle;
  135.             Signal(met,SIGBREAKF_CTRL_D);
  136.         }
  137.     }
  138.  
  139.     CloseDevice(tr);
  140.     DeleteExtIO(tr);
  141.     DeletePort(timerport);
  142.  
  143.     error:
  144.  
  145.     quit_getidle=FALSE;
  146.     start_count =FALSE;
  147.  
  148.     idle   =100000;    /* to avoid divisions by zero from the application */
  149.     maximum=100000;
  150.  
  151.     /* Do nothing and wait for DeleteTask() */
  152.  
  153.     Wait(0L);
  154.  
  155. }
  156.  
  157. setidle()
  158. {
  159.     /* use "__geta4 setidle()" for DICE or SAS and remove geta4(); */
  160.  
  161.     geta4();
  162.  
  163.     if(met=FindTask(NULL))
  164.     if(task=(struct Task *)CreateTask(taskname_1,127,getidle,4096))
  165.     {
  166.         quit_getidle=TRUE;
  167.  
  168.         /* Wait for beginning. Allocating a timerequest may take a while */
  169.  
  170.         while (start_count) cnt=0;                                         
  171.  
  172.         /* maximum counter */
  173.         while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  174.         SetSignal(0,SIGBREAKF_CTRL_D);
  175.  
  176.         met->tc_Node.ln_Pri=-127;
  177.  
  178.         /* idle counter */
  179.         while ((met->tc_SigRecvd & SIGBREAKF_CTRL_D)==0) cnt++;
  180.  
  181.         met->tc_Node.ln_Pri=0;
  182.  
  183.         Signal(task,SIGBREAKF_CTRL_D);
  184.         while(quit_getidle) cnt=0;
  185.  
  186.         /* remove getidle()-task */
  187.  
  188.         Forbid();
  189.         DeleteTask(task);
  190.         Permit();
  191.     }
  192.  
  193.     idle   =100000;    /* to avoid divisions by zero from the       */
  194.     maximum=100000;    /* application, if creation of task failed.  */
  195.  
  196.     quit_setidle=FALSE;
  197.     Wait(0L);
  198. }
  199.  
  200. struct Task *init_idle()
  201. {
  202.     if( task2=(struct Task *)CreateTask(taskname_2,126,setidle,4096))
  203.     {
  204.         Delay(50L); /* To avoid the use of idle and maximum before they're initialized */
  205.     }
  206.     return(task2);
  207. }
  208.  
  209. free_idle()
  210. {
  211.     quit_setidle=TRUE;
  212.     Signal(task2,SIGBREAKF_CTRL_D);
  213.     while(quit_setidle) Delay(10L);
  214.  
  215.     /* remove setidle()-task */
  216.  
  217.     Forbid();
  218.     DeleteTask(task2);
  219.     Permit();
  220. }
  221.